/*
*
*
*	javascript for main page
*	depend on jquery
*
*
*
*
*
*/
$(function()
{
    function imgRollSwipe(el,opt){
        this.options={
            swipeTipObj : $('.imgTipList'),
            swipePosLeft : '-700px',
            swipeIndex : 0,
            swipeDuration : 2000,
            swipeAuto : true
        };
        if(typeof opt=="object"){
            for(var q in opt){
                this.options[q]=opt[q];
            }
        }
        this.element=$(el);
        this._create();
        if(opt.swipeAuto){
            this.autoRun(this.element, this.options.swipePosLeft, this.options.swipeIndex);
        }
    }
    imgRollSwipe.prototype = {
        _create : function() {
            var o = this.options;
            var self = this;
            
            self.pos = {};
            var pos = self.pos;
            pos.t = null;
            
            var $images_box   = $('.imgList');
            var $list_items   = $images_box.children('li');
            var $btn_next     = $('#id-next-img');
            var $btn_prev     = $('#id-prev-img');
            var LEN           = $list_items.length;
            var FIRST         = 0;
            var LAST          = LEN-1;
            var curr_index    = 0;
            $btn_next.bind('click',
                function()
                {
                    if (window.image_show_moving)return;
                    window.image_show_moving = true;
                    clearTimeout(pos.t);
                    pos.t = null;
                    
                    var $first = $images_box.children('li:first');
                    
                    $images_box.append($first.clone())
                        .animate({left:'-700px'},"slow",
                            function()
                            {
                                $first.remove();
                                $images_box.css('left',"0px");
                                self._setActive($images_box);
                                window.image_show_moving = false;
                            }
                        );
                    if(o.swipeAuto){
                        pos.t = setTimeout(function(){self.autoRun(self.element, o.swipePosLeft, curr_index);},3000);
                    }
                    if (curr_index === LAST){
                        curr_index = 0;
                    }else{
                        curr_index++;
                    }
                    
                }
            );
            $btn_prev.bind('click',
                function()
                {
                    if (window.image_show_moving) return;
                    window.image_show_moving = true;
                    clearTimeout(pos.t);
                    pos.t = null;
                    
                    var $last = $images_box.children('li:last');
                    
                    if (curr_index === FIRST){
                        curr_index = LAST;
                    }else{
                        curr_index--;
                    }
                    
                    $images_box.css('left',"-700px")
                        .prepend($last.clone())
                        .animate({left:'0px'},"slow",
                            function()
                            {
                                $last.remove();
                                $images_box.css('left',"0px");
                                self._setActive($images_box);
                                window.image_show_moving = false;
                
                            }
                        );
                    if(o.swipeAuto){
                        pos.t = setTimeout(function(){self.autoRun(self.element, o.swipePosLeft, curr_index);},3000);
                    }
                }
            );
        },
        _setActive : function(obj){
            var o = this.options;
            var self = this;
            var idIndex = obj.children('li:first').attr('data-id');
            $('.cur_'+idIndex).addClass('cur').parent('li').siblings().children().removeClass('cur');
            
        },
        autoRun : function(obj, posLeft, index){
            var o = this.options;
            var self = this;
            pos = this.pos;
            pos.t = setTimeout(function(){
                var $first = obj.children('li:first');
                var last = obj.children('li').length-1;
                obj.append($first.clone())
                    .animate({left:posLeft},"slow",
                        function()
                        {
                            $first.remove();
                            obj.css('left',"0px");
                            self._setActive(obj);
                        }
                    );
                if (index === last){
                    index = 0;
                }else{
                    index++;
                }
                self.autoRun(obj, posLeft, index);
            },o.swipeDuration);
        }
    }
    var opt = {swipeAuto:true};
    new imgRollSwipe($('.imgList'),opt);
});